import classnames from 'classnames'
import Icon from '@/components/Icon'
import styles from './index.module.scss'
import { toRelativeTime } from '@/utils/time'
import Image from '@/components/Image'
import { useDispatch } from 'react-redux'
import { setMoreAction } from '@/store/actions/home'
import { hasToken } from '@/utils/storage'

const ArticleItem = ({ className, article }) => {
  const dispatch = useDispatch()
  const {
    cover: { type, images },
    title,
    aut_name,
    comm_count,
    pubdate,
    art_id,
  } = article
  const token = hasToken()
  return (
    <div className={styles.root}>
      <div
        className={classnames(
          'article-content',
          type === 3 ? 't3' : '',
          type === 0 ? 'none-mt' : ''
        )}
      >
        <h3>{title}</h3>
        {type !== 0 && (
          <div className="article-imgs">
            {images.map((item, i) => (
              <div className="article-img-wrapper" key={i}>
                <Image src={item}></Image>
                {/* <img src={item} alt=""></img> */}
              </div>
            ))}
          </div>
        )}
      </div>
      <div className={classnames('article-info', type === 0 ? 'none-mt' : '')}>
        <span>{aut_name}</span>
        <span>{comm_count} 评论</span>
        <span>{toRelativeTime(pubdate)}</span>

        {token && (
          <span className="close">
            <Icon
              type="iconbtn_essay_close"
              onClick={() =>
                dispatch(
                  setMoreAction({
                    visible: true,
                    articleId: art_id,
                  })
                )
              }
            />
          </span>
        )}
      </div>
    </div>
  )
}

export default ArticleItem
